<?php

namespace app\common\controller;

use app\common\constants\JumpConstants;
use app\common\constants\RsaConstants;
use Curl;
use RSAUtils;

/**
 * 嘉联 - 费率操作
 */
class Rates
{

	use \app\common\traits\ToolTrait;
	use \app\common\traits\RsaTrait;
	/**
	 * 验签 - 参数
	 * @var array
	 */
	private $signPrams = [
		"agentId" 		=> "50265462", // 机构号（嘉联分配）
		"merchNo"		=> "", // 商户号	
		"deviceSn" 		=> "", // 机身号
		"feeCalcType" 	=> "", // 计费类型
		"fixed" 		=> "", // 费率标识，0 : 固定费率，1 : 固定金额，默认0
		"rate" 			=> "", // 费率(%)，费率标识为1 时该字段值表示手续费金额，单位为分
		"signMethod" 	=> "02" // 签名方法，02:RSA私钥签名(SHA256withRSA)
	];

	private $feeList = [];

	/**
	 * 商户费率 - 查询
	 */
	public function query()
	{
		$prams = $this->signPrams;
		$string = $this->argSort($prams);
		$privateKey = RSAUtils::formatterPrivateKey(RsaConstants::JIAL_RSA_PRIVATE_KEY);
		$sign = $this->getSHA256SignWithRSA($string, $privateKey);
		foreach ($prams as $key => $val) {
			if ($val == "" || $val == "null" || $val == null) {
				unset($prams[$key]);
			}
		}
		$prams["signData"] = $sign;
		$responce = $this->http_curl(JumpConstants::JIAL_QUE_RATE_URL_TEST, "post", "JSON", json_encode($prams));
		return $responce;
	}

	/**
	 * 修改
	 */
	public function modify()
	{
		$prams = $this->signPrams;
		$string = $this->argSort($prams);
		$privateKey = RSAUtils::formatterPrivateKey(RsaConstants::JIAL_RSA_PRIVATE_KEY);
		$sign = RSAUtils::rsaSign($string, $privateKey);
		$feeList = [
			"feeCalcType" => $prams["feeCalcType"],
			"fixed" => $prams["fixed"],
			"rate" => $prams["rate"]
		];
		$config = [
			"agentId"  => $prams["agentId"],
			"merchNo" => $prams["merchNo"],
			"deviceSn" => $prams["deviceSn"],
			"feeList"  => (object)$feeList,
			"signMethod" => $prams["signMethod"],
			"signData" => $sign
		];

		$responce = $this->http_curl(JumpConstants::JIAL_MOD_RATE_URL_TEST, "post", "JSON", json_encode($config));
		return $responce;
	}

	/**
	 * 预设 
	 * agentId,deviceSn,feeList.feeCalcType,feeList.fixed,feeList.rate,signMethod
	 */
	public function pre_sets()
	{
		$prams = $this->signPrams;
		$string = $this->argSort($prams);
		$privateKey = RSAUtils::formatterPrivateKey(RsaConstants::JIAL_RSA_PRIVATE_KEY);
		// $privatekey = openssl_pkey_get_private($privateKey);
		// $sign = RSAUtils::rsaSign($string, $privateKey);
		$sign = $this->getSHA256SignWithRSA($string, $privateKey);
		$feeList = [
			"feeCalcType" => $prams["feeCalcType"],
			"fixed" => $prams["fixed"],
			"rate" => $prams["rate"]
		];
		$config = [
			"agentId"  => $prams["agentId"],
			"deviceSn" => $prams["deviceSn"],
			"feeList"  => (object)$feeList,
			"signMethod" => $prams["signMethod"],
			"signData" => $sign
		];
		$responce = $this->http_curl(JumpConstants::JIAL_PRE_RATE_URL_TEST, "post", "JSON", json_encode($config));
		return $responce;
	}

	/**
	 * 对数组排序
	 * @param $param
	 * @return mixed 排序后的数组
	 */

	public function argSort($param)
	{
		ksort($param);
		reset($param);
		return $this->createLinkString($param);
	}

	/**
	 * 把数组所有元素，按照“参数=参数值”的模式用“&”字符拼接成字符串
	 * @param $para
	 * @return bool|string 拼接完成以后的字符串
	 */

	public function createLinkString($para)
	{
		$arg = "";
		foreach ($para as $key => $vals) {
			$arg .=  $vals;
		}
		return $arg;
	}

	/**
	 * 设置 - 参数
	 */
	public function set($key, $vals)
	{
		$this->signPrams[$key] = $vals;
	}
}
